<?php
namespace app\massage\model;

use app\adapay\model\Member;
use app\BaseModel;
use app\coachbroker\model\CoachBroker;
use think\facade\Db;

class Commission extends BaseModel
{


    //type 1分销 2加盟商 3技师 4分销商 5上级分销商 6省代分销 7技师拉用户充值余额 8车费 9合伙人 10渠道商 11平台 12业务员 13车费（代理商）14 二级分销 15分销推荐佣金
    //定义表名
    protected $name = 'massage_service_order_commission';


    protected $append = [

        'order_goods',

        'point_cash'

    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:手续费
     */
    public function getPointCashAttr($value, $data)
    {

        if (!empty($data['id'])) {

            $share_model = new CommShare();

            $point_cash = $share_model->where(['comm_id' => $data['id'], 'cash_type' => 1])->sum('share_cash');

            return $point_cash;
        }
    }


    /**
     * @param $value
     * @param $data
     * @功能说明:保留两位小数
     */
    public function getCashAttr($value, $data)
    {

        if (isset($value)) {

            return round($value, 2);
        }
    }


    /**
     * @param $value
     * @param $data
     * @功能说明:
     */
    public function getOrderGoodsAttr($value, $data)
    {

        if (!empty($data['id'])) {

            $order_goods_model = new CommissionGoods();

            $list = $order_goods_model->goodsList(['a.commission_id' => $data['id']]);

            return $list;

        }

    }


    /**
     * @功能说明:记录15.51
     * 2099.82
     */
    public function recordList($dis, $page = 10)
    {

        $data = $this->alias('a')
            ->join('massage_service_user_list b', 'a.user_id = b.id', 'left')
            ->join('massage_service_user_list c', 'a.top_id = c.id', 'left')
            ->join('massage_service_order_list d', 'a.order_id = d.id', 'left')
            ->where($dis)
            ->field('a.*,b.nickName,c.nickName as top_name,d.order_code,d.pay_type,d.pay_price,d.transaction_id,d.car_price,d.material_type,d.material_price,d.discount')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();

        return $data;

    }


    /**
     * @功能说明:记录
     */
    public function recordListV2($dis, $where, $page = 10)
    {

        $data = $this->alias('a')
            ->join('massage_service_user_list c', 'a.top_id = c.id  AND a.type in (1,14)', 'left')
            ->join('massage_service_order_list d', 'a.order_id = d.id')
            ->join('shequshop_school_admin e', 'a.top_id = e.id AND a.type in (2,5,6,11,13)', 'left')
            ->join('massage_service_coach_list f', 'a.top_id = f.id AND a.type in (3,8)', 'left')
            ->join('massage_order_coach_change_logs g', 'if(d.is_add=0,d.id,d.add_pid) = g.order_id AND g.is_new = 1 AND a.top_id=0 AND a.type in (3,8) ', 'left')
            ->join('massage_channel_list h', 'a.top_id = h.id AND a.type = 10', 'left')
            ->join('massage_salesman_list m', 'a.top_id = m.id AND a.type = 12', 'left')
            ->join('massage_coach_broker_list i', 'a.broker_id = i.id AND a.type = 9', 'left')
            ->where($dis)
            ->where(function ($query) use ($where)
            {
                $query->whereOr($where);
            })
            ->field('a.*,m.user_name as salesman_name,h.user_name as channel_name,e.agent_name as admin_name,f.coach_name,c.nickName as top_name,d.order_code,d.pay_type,d.pay_price,d.transaction_id,d.car_price,d.material_price,g.now_coach_name,i.user_name as broker_name')
            ->group('a.id')
            ->order('d.id desc,a.id desc')
            ->paginate($page)
            ->toArray();

        return $data;

    }


    /**
     * @功能说明:记录
     */
    public function recordSelect($dis, $where)
    {

        $data = $this->alias('a')
            ->join('massage_service_user_list c', 'a.top_id = c.id  AND a.type in (1,14)', 'left')
            ->join('massage_service_order_list d', 'a.order_id = d.id')
            ->join('shequshop_school_admin e', 'a.top_id = e.id AND a.type in (2,5,6,11,13)', 'left')
            ->join('massage_service_coach_list f', 'a.top_id = f.id AND a.type in (3,8)', 'left')
            ->join('massage_order_coach_change_logs g', 'if(d.is_add=0,d.id,d.add_pid) = g.order_id AND g.is_new = 1 AND a.top_id=0 AND a.type in (3,8) ', 'left')
            ->join('massage_channel_list h', 'a.top_id = h.id AND a.type = 10', 'left')
            ->join('massage_salesman_list m', 'a.top_id = m.id AND a.type = 12', 'left')
            ->join('massage_coach_broker_list i', 'a.broker_id = i.id AND a.type = 9', 'left')
            ->where($dis)
            ->where(function ($query) use ($where)
            {
                $query->whereOr($where);
            })
            ->field('a.*,m.user_name as salesman_name,h.user_name as channel_name,e.agent_name as admin_name,f.coach_name,c.nickName as top_name,d.order_code,d.pay_type,d.pay_price,d.transaction_id,d.car_price,g.now_coach_name,d.material_price,i.user_name as broker_name')
            ->group('a.id')
            ->order('d.id desc,a.id desc')
            ->select()
            ->toArray();

        return $data;

    }


    /**
     * @功能说明:佣金到账
     */
    public function successCash($order_id)
    {

        $data = $this->dataInfo(['order_id' => $order_id, 'type' => 1]);

        if (!empty($data) && $data['status'] == 1 && $data['cash'] > 0) {

            $user_model = new User();

            $user = $user_model->dataInfo(['id' => $data['top_id']]);

            $res = $user_model->where(['id' => $data['top_id'], 'new_cash' => $user['new_cash']])->update(['new_cash' => $user['new_cash'] + $data['cash'], 'cash' => $user['cash'] + $data['cash']]);

            if ($res == 0) {

                return $res;
            }

        }

        return 1;
    }


    /**
     * @功能说明:代理商以及上级代理佣金到账
     */
    public function adminSuccessCash($order_id)
    {

        $dis = [

            'order_id' => $order_id,

            'status' => 1
        ];

        $data = $this->where($dis)->where('type', 'in', [2, 5, 6])->select()->toArray();

        $admin_model = new Admin();

        if (!empty($data)) {

            foreach ($data as $v) {

                $cash = $v['cash'];

                $admin = $admin_model->dataInfo(['id' => $v['admin_id']]);

                if (!empty($admin) && $cash > 0) {

                    $res = $admin_model->where(['id' => $v['admin_id'], 'cash' => $admin['cash']])->update(['cash' => Db::raw("cash+$cash")]);

                    if ($res == 0) {

                        return $res;
                    }
                }

            }

        }
        //结束所有佣金
        $this->dataUpdate(['order_id' => $order_id], ['status' => 2, 'cash_time' => time()]);

        return true;

    }


    /**
     * @功能说明:佣金到账
     */

    public function commissionSucessCash($order_id)
    {

        $dis = [

            'order_id' => $order_id,

            'status' => 1
        ];

        $data = $this->where($dis)->where('type', '<>', 11)->where('type', '<>', 7)->select()->toArray();

        $user_model = new User();

        $admin_model = new Admin();

        $channel_model = new ChannelList();

        $salesman_model = new Salesman();

        $member_model = new Member();

        $coach_model = new Coach();

        $broker_model = new CoachBroker();

        if (!empty($data)) {

            foreach ($data as $v) {
                //结束所有佣金
                $res = $this->dataUpdate(['id' => $v['id']], ['status' => 2, 'cash_time' => time()]);

                if ($res == 0) {

                    return $res;
                }

                $cash = $v['cash'];

                if ($cash <= 0) {

                    continue;
                }

                if (in_array($v['type'], [1, 9, 14])) {

                    if ($v['type'] == 9 && !empty($v['broker_id'])) {

                        $broker_info = $broker_model->dataInfo(['id' => $v['broker_id']]);

                        if (!empty($broker_info)) {

                            $res = $broker_model->where(['id' => $v['broker_id'], 'cash' => $broker_info['cash']])->update(['cash' => $broker_info['cash'] + $cash]);

                            if ($res == 0) {

                                return $res;
                            }
                        }

                    } else {
                        //用户分销 合伙人
                        $user = $user_model->dataInfo(['id' => $v['top_id']]);

                        if (!empty($user)) {

                            $res = $user_model->where(['id' => $v['top_id'], 'new_cash' => $user['new_cash']])->update(['new_cash' => $user['new_cash'] + $cash, 'cash' => $user['cash'] + $cash]);

                            if ($res == 0) {

                                return $res;
                            }
                        }
                    }

                } elseif (in_array($v['type'], [2, 5, 6, 13])) {
                    //代理商
                    $admin = $admin_model->dataInfo(['id' => $v['admin_id']]);

                    if (!empty($admin)) {

                        $res = $admin_model->where(['id' => $v['admin_id'], 'cash' => $admin['cash']])->update(['cash' => $admin['cash'] + $cash]);

                        if ($res == 0) {

                            return $res;
                        }
                    }
                } elseif (in_array($v['type'], [10])) {
                    //渠道商
                    $channel = $channel_model->dataInfo(['id' => $v['top_id']]);

                    if (!empty($channel)) {

                        $res = $channel_model->where(['id' => $v['top_id']])->update(['total_cash' => Db::raw("total_cash+$cash"), 'cash' => Db::raw("cash+$cash")]);

                        if ($res == 0) {

                            return $res;
                        }
                    }

                } elseif (in_array($v['type'], [12])) {
                    //业务员
                    $salesman = $salesman_model->dataInfo(['id' => $v['top_id']]);

                    if (!empty($salesman)) {

                        $res = $salesman_model->where(['id' => $v['top_id']])->update(['total_cash' => Db::raw("total_cash+$cash"), 'cash' => Db::raw("cash+$cash")]);

                        if ($res == 0) {

                            return $res;
                        }

                    }

                } elseif (in_array($v['type'], [8])) {

                    $res = $coach_model->where(['id' => $v['top_id']])->update(['car_price' => Db::raw("car_price+$cash")]);

                    if ($res == 0) {

                        return $res;
                    }
                }
                //分账
                $member_model->adapayCommission($v, $order_id);
            }
        }
        //给平台分账
        $member_model->adaPayMyself($order_id);

        return true;
    }


    /**
     * @功能说明:渠道商代理佣金到账
     */
    public function channelSuccessCash($order_id)
    {

        $dis = [

            'order_id' => $order_id,

            'status' => 1
        ];

        $data = $this->where($dis)->where('type', 'in', [2, 5, 6])->select()->toArray();

        $admin_model = new Admin();

        if (!empty($data)) {

            foreach ($data as $v) {

                $cash = $v['cash'];

                $admin = $admin_model->dataInfo(['id' => $v['admin_id']]);

                if (!empty($admin) && $cash > 0) {

                    $res = $admin_model->where(['id' => $v['admin_id'], 'cash' => $admin['cash']])->update(['cash' => Db::raw("cash+$cash")]);

                    if ($res == 0) {

                        return $res;
                    }
                }

            }

        }
        //结束所有佣金
        $this->dataUpdate(['order_id' => $order_id], ['status' => 2, 'cash_time' => time()]);

        return true;

    }


    /**
     * @功能说明:添加
     */
    public function dataAdd($data)
    {

        $data['create_time'] = !empty($data['create_time']) ? $data['create_time'] : time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @功能说明:编辑
     */
    public function dataUpdate($dis, $data)
    {

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @功能说明:列表
     */
    public function dataList($dis, $page = 10)
    {

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @功能说明:
     */
    public function dataInfo($dis)
    {

        $data = $this->where($dis)->find();

        return !empty($data) ? $data->toArray() : [];

    }


    /**
     * @功能说明:添加佣金
     */
    public function commissionAdd($order)
    {
        //技师佣金
        $this->coachCommission($order);
        //加盟商佣金
        $this->adminCommission($order);

        $user_model = new User();
        //上级
        $top = $user_model->where(['id' => $order['user_id']])->value('pid');

        if (!empty($top)) {

            $ser_model = new Service();

            $com_mdoel = new Commission();

            $com_goods_mdoel = new CommissionGoods();

            foreach ($order['order_goods'] as $v) {
                //查看是否有分销
                $ser = $ser_model->dataInfo(['id' => $v['goods_id']]);

                if (!empty($ser['com_balance'])) {

                    $insert = [

                        'uniacid' => $order['uniacid'],

                        'user_id' => $order['user_id'],

                        'top_id' => $top,

                        'order_id' => $order['id'],

                        'order_code' => $order['order_code'],

                    ];

                    $find = $com_mdoel->dataInfo($insert);

                    $cash = $v['true_price'] * $ser['com_balance'] / 100 * $v['num'];

                    if (empty($find)) {

                        $insert['cash'] = $cash;

                        $com_mdoel->dataAdd($insert);

                        $id = $com_mdoel->getLastInsID();

                    } else {

                        $id = $find['id'];

                        $update = [

                            'cash' => $find['cash'] + $cash
                        ];
                        //加佣金
                        $com_mdoel->dataUpdate(['id' => $find['id']], $update);

                    }

                    $insert = [

                        'uniacid' => $order['uniacid'],

                        'order_goods_id' => $v['id'],

                        'commission_id' => $id,

                        'cash' => $cash,

                        'num' => $v['num'],

                        'balance' => $ser['com_balance']
                    ];
                    //添加到自订单记录表
                    $res = $com_goods_mdoel->dataAdd($insert);

                }

            }

        }

        return true;

    }


    /**
     * @功能说明:添加佣金
     */
    public function commissionAddData($order)
    {

        $user_model = new User();

//        $config_model = new Config();
//
//        $config = $config_model->dataInfo(['uniacid'=>$order['uniacid']]);

        $dis = [

            'id' => $order['user_id'],

        ];
        //上级
        $top_id = $user_model->where($dis)->value('pid');

        $top = $user_model->dataInfo(['id' => $top_id]);

        $total_cash = 0;

        if (!empty($top)) {

            if (getFxStatus($order['uniacid']) == 1 && $top['is_fx'] == 0) {

                return $total_cash;
            }

            $top = $top['id'];

            $ser_model = new Service();

            $com_mdoel = new Commission();

            $com_goods_mdoel = new CommissionGoods();

            foreach ($order['order_goods'] as $v) {
                //查看是否有分销
                $ser = $ser_model->dataInfo(['id' => $v['goods_id']]);

                $user_agent_balance = 0;

                if ($ser['com_balance'] <= 0) {
                    //全局设置
                    $ser['com_balance'] = getConfigSetting($order['uniacid'], 'user_agent_balance');

                    $user_agent_balance = 1;
                }

                if (!empty($ser['com_balance'])) {

                    $insert = [

                        'uniacid' => $order['uniacid'],

                        'user_id' => $order['user_id'],

                        'top_id' => $top,

                        'order_id' => $order['id'],

                        'order_code' => $order['order_code'],

                        'balance' => $user_agent_balance == 1 ? $ser['com_balance'] : 0,

                    ];

                    $find = $com_mdoel->dataInfo($insert);

                    $cash = $v['true_price'] * $ser['com_balance'] / 100 * $v['num'];

                    $total_cash += $cash;

                    if (empty($find)) {

                        $insert['cash'] = $cash;

                        $insert['status'] = -1;

                        $insert['admin_id'] = !empty($order['admin_id']) ? $order['admin_id'] : 0;

                        $com_mdoel->dataAdd($insert);

                        $id = $com_mdoel->getLastInsID();

                    } else {

                        $id = $find['id'];

                        $update = [

                            'cash' => $total_cash
                        ];
                        //加佣金
                        $com_mdoel->dataUpdate(['id' => $find['id']], $update);

                    }

                    $insert = [

                        'uniacid' => $order['uniacid'],

                        'order_goods_id' => $v['id'],

                        'commission_id' => $id,

                        'cash' => $cash,

                        'num' => $v['num'],

                        'balance' => $ser['com_balance']
                    ];
                    //添加到自订单记录表
                    $res = $com_goods_mdoel->dataAdd($insert);

                }

            }

        }

        return $total_cash;

    }


    /**
     * @param $order
     * @功能说明:技师佣金
     */
    public function carCommission($order, $status = -1)
    {

        if (isset($order['true_car_price']) && $order['true_car_price'] > 0 && $order['free_fare'] != 2) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [8, 13])->find();

            if (!empty($find)) {

                return false;
            }

            if (!empty($order['caradmin'])) {

                $insert = [

                    'uniacid' => $order['uniacid'],

                    'user_id' => $order['user_id'],

                    'top_id' => $order['admin_id'],

                    'order_id' => $order['id'],

                    'order_code' => $order['order_code'],

                    'type' => 13,

                    'cash' => $order['true_car_price'],

                    'admin_id' => $order['admin_id'],

                    'balance' => 0,

                    'status' => $status,

                    'cash_time' => time(),

                    'update_time' => time(),
                ];

            } else {

                $insert = [

                    'uniacid' => $order['uniacid'],

                    'user_id' => $order['user_id'],

                    'top_id' => $order['coach_id'],

                    'order_id' => $order['id'],

                    'order_code' => $order['order_code'],

                    'type' => 8,

                    'cash' => $order['true_car_price'],

                    'admin_id' => $order['admin_id'],

                    'balance' => 0,

                    'status' => $status,

                    'cash_time' => time(),

                    'update_time' => time(),
                ];
            }

            if (empty($order['coach_id'])) {

                $insert['cash_status'] = 0;
            }

            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $res = $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $share_model = new CommShare();
            //添加手续费
            $share_model->addPointData($id, $order);

        }

        return true;
    }

    /**
     * @param $order
     * @功能说明:技师佣金
     */
    public function coachCommission($order)
    {

        if (isset($order['coach_cash'])) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [3])->find();

            if (!empty($find)) {

                return false;
            }

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id' => $order['coach_id'],

                'order_id' => $order['id'],

                'order_code' => $order['order_code'],

                'type' => 3,

                'cash' => $order['coach_cash'],

                'admin_id' => $order['admin_id'],

                'balance' => $order['coach_balance'],

                'status' => -1,
            ];

            if (empty($order['coach_id'])) {

                $insert['cash_status'] = 0;
            }

            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $res = $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $share_model = new CommShare();
            //添加手续费
            $share_model->addPointData($id, $order);
            //添加广告费
            $share_model->addPosterData($id, $order, 1);
            //储值扣款
            $share_model->coachBalanceCash($id, $order);
            //技术服务费
            $share_model->coachSkillCash($id, $order);

            return $res;
        }

        return 0;
    }


    /**
     * @功能说明:加盟商佣金
     */
    public function adminCommission($order)
    {

        if (!empty($order['admin_id'])) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [2])->find();

            if (!empty($find)) {

                return false;
            }

            $admin_model = new Admin();

            $city_type = $admin_model->where(['id' => $order['admin_id']])->value('city_type');

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id' => $order['admin_id'],

                'order_id' => $order['id'],

                'order_code' => $order['order_code'],

                'type' => 2,

                'cash' => $order['admin_cash'],

                'admin_id' => $order['admin_id'],

                'balance' => $order['admin_balance'],

                'status' => -1,

                'city_type' => $city_type,

            ];
            //如果是线下技师需要把佣金返回给代理商
            if (empty($order['coach_id'])) {

                $insert['coach_cash'] = $order['coach_cash'];

                $insert['car_cash'] = $order['true_car_price'];

            }

            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $res = $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $share_model = new CommShare();
            //添加手续费
            $share_model->addPointData($id, $order);
            //添加广告费
            $share_model->addPosterData($id, $order, 2);
        }

        return true;

    }


    /**
     * @功能说明:加盟商上级佣金
     */
    public function adminLevelCommission($order)
    {

        if (!empty($order['admin_pid'])) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [5])->find();

            if (!empty($find)) {

                return false;
            }

            $admin_model = new Admin();

            $city_type = $admin_model->where(['id' => $order['admin_pid']])->value('city_type');

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id' => $order['admin_pid'],

                'order_id' => $order['id'],

                'order_code' => $order['order_code'],

                'type' => 5,

                'cash' => $order['level_cash'],

                'admin_id' => $order['admin_pid'],

                'balance' => $order['level_balance'],

                'status' => -1,

                'city_type' => $city_type,

            ];
            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $res = $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $share_model = new CommShare();
            //添加手续费
            $share_model->addPointData($id, $order);

        }

        return true;

    }


    /**
     * @功能说明:省代理商佣金
     */
    public function adminProvinceCommission($order)
    {

        if (!empty($order['p_admin_pid'])) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [6])->find();

            if (!empty($find)) {

                return false;
            }

            $admin_model = new Admin();

            $city_type = $admin_model->where(['id' => $order['p_admin_pid']])->value('city_type');

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id' => $order['p_admin_pid'],

                'order_id' => $order['id'],

                'order_code' => $order['order_code'],

                'type' => 6,

                'cash' => $order['p_level_cash'],

                'admin_id' => $order['p_admin_pid'],

                'balance' => $order['p_level_balance'],

                'status' => -1,

                'city_type' => $city_type,

            ];
            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $res = $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $share_model = new CommShare();
            //添加手续费
            $share_model->addPointData($id, $order);

        }

        return true;

    }

    /**
     * @功能说明:佣金到账
     */
    public function successCommission($order_id)
    {

        $comm = $this->dataInfo(['order_id' => $order_id, 'status' => 1]);

        if (!empty($comm)) {

            $user_model = new User();

            $user = $user_model->dataInfo(['id' => $comm['top_id']]);

            if (!empty($user)) {

                $update = [

                    'balance' => $user['balance'] + $comm['cash'],

                    'cash' => $user['cash'] + $comm['cash'],
                ];

                $user_model->dataUpdate(['id' => $comm['top_id']], $update);

                $this->dataUpdate(['id' => $comm['id']], ['status' => 2, 'cash_time' => time()]);

            }

        }

        return true;

    }


    /**
     * @功能说明:退款的时候要减去分销
     */
    public function refundComm($refund_id)
    {

        $refund_model = new RefundOrder();

        $com_goods_mdoel = new CommissionGoods();

        $order_model = new Order();

        $refund_order = $refund_model->dataInfo(['id' => $refund_id]);

        if (!empty($refund_order)) {
            //查询这笔等待有无佣金
            $comm = $this->dataInfo(['order_id' => $refund_order['order_id'], 'status' => 1, 'type' => 1]);

            if (!empty($comm)) {

                foreach ($refund_order['order_goods'] as $v) {

                    $comm_goods = $com_goods_mdoel->dataInfo(['commission_id' => $comm['id'], 'order_goods_id' => $v['order_goods_id']]);

                    if (!empty($comm_goods)) {

                        $comm_goods_cash = $comm_goods['cash'] / $comm_goods['num'];

                        $true_num = $comm_goods['num'] - $v['num'];

                        $true_num = $true_num > 0 ? $true_num : 0;

                        $update = [

                            'num' => $true_num,

                            'cash' => $comm_goods_cash * $true_num
                        ];

                        $com_goods_mdoel->dataUpdate(['id' => $comm_goods['id']], $update);
                    }

                }

                $total_cash = $com_goods_mdoel->where(['commission_id' => $comm['id']])->sum('cash');

                $total_cash = $total_cash > 0 ? $total_cash : 0;

                $update = [

                    'cash' => $total_cash,

                    'status' => $total_cash > 0 ? 1 : -1
                ];

                $this->dataUpdate(['id' => $comm['id']], $update);

                $order_model->dataUpdate(['id' => $refund_order['order_id']], ['user_cash' => $total_cash]);

            }

        }

        return true;

    }


    /**
     * @param $v
     * @param $pay_order
     * @功能说明:
     */
    public function adminCashCustom($v, $pay_order)
    {

        if ($v['type'] == 2) {

            if ($v['city_type'] == 1) {

                $pay_order['city_balance'] = $v['balance'];

                $pay_order['admin_balance_name'] = 'city_balance';

                $pay_order['admin_cash_name'] = 'city_cash';
            } else {

                $pay_order['district_balance'] = $v['balance'];

                $pay_order['admin_balance_name'] = 'district_balance';

                $pay_order['admin_cash_name'] = 'district_cash';
            }

        }

        if ($v['type'] == 5) {

            $pay_order['city_balance'] = $v['balance'];

            $pay_order['level_balance_name'] = 'city_balance';

            $pay_order['level_cash_name'] = 'city_cash';
        }

        return $pay_order;

    }


    /**
     * @param $order
     * @功能说明:获取主订单的各类佣金比例
     */
    public function getCommBalance($pay_order)
    {

        $level_cash_data = $this->where(['order_id' => $pay_order['id'], 'status' => 1])->where('type', 'in', [1, 2, 3, 5, 6, 9, 10, 11, 12, 14])->select()->toArray();

        $share_model = new CommShare();

        foreach ($level_cash_data as $v) {

            $cash_text = $this->getTypeText($v['type']);

            if (in_array($v['type'], [5, 6])) {

                $pay_order[$cash_text['admin_id']] = $v['admin_id'];
            }

            if (in_array($v['type'], [9, 10, 12])) {

                $pay_order[$cash_text['admin_id']] = $v['top_id'];

                $pay_order[$cash_text['coach_share_balance']] = $share_model->getShareBalance($v['id'], 1);

                $pay_order[$cash_text['admin_share_balance']] = $share_model->getShareBalance($v['id'], 2);

                $pay_order[$cash_text['company_share_balance']] = $share_model->getShareBalance($v['id'], 3);
            }

            $pay_order[$cash_text['balance']] = $v['balance'];

            $pay_order = $this->adminCashCustom($v, $pay_order);
        }

        return $pay_order;
    }


    /**
     * @功能说明:订单退款修改佣金记录
     */
    //type 1分销 2加盟商 3技师 4分销商 5上级分销商 6省代分销 7技师拉用户充值余额 8车费 9合伙人 10渠道商 11平台 12业务员 13车费（代理商）
    public function refundCash($pay_order, $comm_type = 2)
    {

        $order_model = new Order();

        $share_model = new CommShare();

        $level_cash_data = $this->where(['order_id' => $pay_order['id'], 'status' => 1])->where('type', 'in', [1, 2, 3, 5, 6, 9, 10, 11, 12, 14])->select()->toArray();

        $pay_order = $this->getCommBalance($pay_order);
        //升级订单
        if ($comm_type == 3) {

            $user_model = new User();
            //分销
            $pay_order = $user_model->getUserPid($pay_order);
        }
        //查询有无二级市级代理佣金或者省代
        if (!empty($level_cash_data)) {
            //修改佣金信息
            $cash_data = $order_model->getCashData($pay_order, $comm_type);

            $cash_data = $cash_data['data'];

            $cash_order_update = [

                'admin_cash' => $cash_data['admin_cash'],

                'coach_cash' => $cash_data['coach_cash'],

                'company_cash' => $cash_data['company_cash'],

                'user_cash' => $cash_data['user_c_cash'],
            ];

            $arr = [1, 2, 3, 5, 6, 9, 10, 11, 12, 14];

            foreach ($arr as $value) {

                $cash_text = $this->getTypeText($value);

                if (key_exists($cash_text['cash'], $cash_data)) {

                    $update = [

                        'cash' => $cash_data[$cash_text['cash']]
                    ];
                    //修改各类佣金记录
                    $this->dataUpdate(['order_id' => $pay_order['id'], 'type' => $value, 'status' => 1], $update);

                    $comm_data = $this->dataInfo(['order_id' => $pay_order['id'], 'type' => $value, 'status' => 1]);

                    if (!empty($comm_data)) {
                        //渠道商 合伙人 业务员 对应的分摊金额可能会变
                        if (in_array($value, [9, 10, 12])) {

                            if (key_exists($cash_text['coach_share_cash'], $cash_data)) {

                                $share_model->dataUpdate(['comm_id' => $comm_data['id'], 'type' => 1], ['share_cash' => $cash_data[$cash_text['coach_share_cash']]]);
                            }

                            if (key_exists($cash_text['admin_share_cash'], $cash_data)) {

                                $share_model->dataUpdate(['comm_id' => $comm_data['id'], 'type' => 2], ['share_cash' => $cash_data[$cash_text['admin_share_cash']]]);
                            }

                            if (key_exists($cash_text['company_share_cash'], $cash_data)) {

                                $share_model->dataUpdate(['comm_id' => $comm_data['id'], 'type' => 3], ['share_cash' => $cash_data[$cash_text['company_share_cash']]]);
                            }
                        }

                        if (key_exists($cash_text['point'], $cash_data)) {
                            //修改手续费
                            $share_model->dataUpdate(['comm_id' => $comm_data['id'], 'cash_type' => 1], ['share_cash' => $cash_data[$cash_text['point']]]);
                        }
                        //修改广告分摊
                        if (in_array($value, [2, 3, 11]) && key_exists($cash_text['poster'], $cash_data)) {

                            $share_model->dataUpdate(['comm_id' => $comm_data['id'], 'cash_type' => 2], ['share_cash' => $cash_data[$cash_text['poster']]]);
                        }
                        //储值扣款和技术服务费
                        if (in_array($value, [3])) {

                            if (key_exists($cash_text['comm_coach_balance'], $cash_data)) {

                                $share_model->dataUpdate(['comm_id' => $comm_data['id'], 'cash_type' => 3], ['share_cash' => $cash_data[$cash_text['comm_coach_balance']]]);
                            }

                            if (key_exists($cash_text['skill_balance'], $cash_data)) {

                                $share_model->dataUpdate(['comm_id' => $comm_data['id'], 'cash_type' => 4], ['share_cash' => $cash_data[$cash_text['skill_balance']]]);
                            }
                        }
                    }
                }
            }

            $res = $order_model->dataUpdate(['id' => $pay_order['id']], $cash_order_update);

            return $res;
        }

        return true;

    }


    /**
     * @param $type
     * @功能说明:
     */
    public function getTypeText($type)
    {

        switch ($type) {

            case 1:
                $arr['cash'] = 'user_c_cash';

                $arr['balance'] = 'user_agent_balance';

                $arr['point'] = 'user_point_cash';

                break;

            case 2:
                $arr['cash'] = 'admin_cash';

                $arr['balance'] = 'admin_balance';

                $arr['admin_id'] = 'admin_pid';

                $arr['point'] = 'admin_point_cash';
                //广告分摊
                $arr['poster'] = 'share_poster_admin';

                break;
            case 3:
                $arr['cash'] = 'coach_cash';

                $arr['balance'] = 'coach_balance';

                $arr['point'] = 'coach_point_cash';
                //广告分摊
                $arr['poster'] = 'share_poster_coach';

                $arr['comm_coach_balance'] = 'comm_coach_cash';

                $arr['skill_balance'] = 'skill_cash';

                break;

            case 5:
                $arr['cash'] = 'level_cash';

                $arr['balance'] = 'level_balance';

                $arr['admin_id'] = 'admin_pid';

                $arr['point'] = 'city_point_cash';

                break;

            case 6:
                $arr['cash'] = 'p_level_cash';

                $arr['balance'] = 'p_level_balance';

                $arr['admin_id'] = 'p_admin_pid';

                $arr['point'] = 'province_point_cash';

                break;

            case 8:

                $arr['cash'] = 'true_car_price';

                $arr['point'] = 'car_point_cash';

                break;
            case 9:
                $arr['cash'] = 'partner_cash';

                $arr['balance'] = 'coach_agent_balance';

                $arr['admin_id'] = 'partner_id';

                $arr['coach_share_balance'] = 'partner_coach_balance';

                $arr['admin_share_balance'] = 'partner_admin_balance';

                $arr['company_share_balance'] = 'partner_company_balance';

                $arr['coach_share_cash'] = 'partner_coach_cash';

                $arr['admin_share_cash'] = 'partner_admin_cash';

                $arr['company_share_cash'] = 'partner_company_cash';

                $arr['point'] = 'partner_point_cash';

                break;
            case 10:
                $arr['cash'] = 'channel_cash';

                $arr['balance'] = 'channel_balance';

                $arr['admin_id'] = 'channel_id';

                $arr['coach_share_balance'] = 'channel_coach_balance';

                $arr['admin_share_balance'] = 'channel_admin_balance';

                $arr['company_share_balance'] = 'channel_company_balance';

                $arr['coach_share_cash'] = 'channel_coach_cash';

                $arr['admin_share_cash'] = 'channel_admin_cash';

                $arr['company_share_cash'] = 'channel_company_cash';

                $arr['point'] = 'channel_point_cash';

                break;

            case 11:
                $arr['cash'] = 'company_cash';

                $arr['balance'] = 'company_balance';

                $arr['point'] = 'company_point_cash';
                //广告分摊
                $arr['poster'] = 'share_poster_company';

                break;

            case 12:
                $arr['cash'] = 'salesman_cash';

                $arr['balance'] = 'salesman_balance';

                $arr['admin_id'] = 'salesman_id';

                $arr['coach_share_balance'] = 'salesman_coach_balance';

                $arr['admin_share_balance'] = 'salesman_admin_balance';

                $arr['company_share_balance'] = 'salesman_company_balance';

                $arr['coach_share_cash'] = 'salesman_coach_cash';

                $arr['admin_share_cash'] = 'salesman_admin_cash';

                $arr['company_share_cash'] = 'salesman_company_cash';

                $arr['point'] = 'salesman_point_cash';

                break;
            case 13:

                $arr['cash'] = 'true_car_price';

                $arr['point'] = 'car_point_cash';

                break;

            case 14:
                $arr['cash'] = 'level_reseller_cash';

                $arr['balance'] = 'level_reseller_balance';

                $arr['point'] = 'level_reseller_point_cash';

                break;
            case 15:

                $arr['point'] = 'inv_reseller_point_cash';

                break;
            default:

                $arr = [];
        }

        return $arr;
    }


    /**
     * @功能说明:获取佣金相关参数
     */
    public function commissionData($type = 1)
    {

        $arr = [
            //技师佣金
            [

                'action_name' => "getCoachCash",

                'parameter' => 'coach_balance',

                'point' => 'coach_point_cash',

            ],//用户分销
            [


                'action_name' => $type != 2 ? 'getUserCash' : 'getUserCashRefund',

                'parameter' => 'user_agent_balance',

                'point' => 'user_point_cash'
            ],
            //平台抽成
            [

                'action_name' => 'getCompanyCash',

                'parameter' => 'admin_balance',

                'point' => 'company_point_cash'
            ],
            //省代
            [

                'action_name' => 'getProvinceCash',

                'parameter' => 'p_level_balance',

                'point' => 'province_point_cash'


            ],//城市代理
            [

                'action_name' => 'getCityCash',

                'parameter' => 'level_balance',

                'point' => 'city_point_cash'

            ],
            //合伙人
            [

                'action_name' => 'getPartnerCash',

                'parameter' => 'coach_agent_balance',

                'point' => 'partner_point_cash'

            ],
            //渠道商
            [

                'action_name' => 'getChannelCash',

                'parameter' => 'channel_balance',

                'point' => 'channel_point_cash'

            ],
            //业务员
            [

                'action_name' => 'getSalesmanCash',

                'parameter' => 'salesman_balance',

                'point' => 'salesman_point_cash'

            ],

        ];

        return $arr;
    }


    /**
     * @param $order
     * @功能说明:计算各类分销比例
     */
    public function balanceData($order, $admin_id = 0)
    {

        $coach_model = new Coach();

        $admin_model = new Admin();

        $clock_model = new ClockSetting();

        $channel_model = new ChannelList();

        $custom_model = new CustomBalance();

        $user_model = new User();

        $coach = $coach_model->dataInfo(['id' => $order['coach_id']]);

        $coach_level = $custom_model->getCoachCustomBalance($order['coach_id']);

        if (empty($coach_level)) {
            //技师等级
            $coach_level = $coach_model->getCoachLevel($order['coach_id'], $order['uniacid']);

            if (empty($coach_level) && !empty($order['coach_id'])) {

                return ['code' => 300];
            }
        }

        $coach_level['balance'] = !empty($coach_level) ? $coach_level['balance'] : 0;
        //技师佣金比列
        $order['coach_balance'] = $coach_level['balance'];

        $order['admin_id'] = 0;

        $admin_id = !empty($coach['admin_id']) ? $coach['admin_id'] : $admin_id;
        //代理商各类分销比例
        $order = $admin_model->agentBalanceData($admin_id, $order);
        //加钟的时候比例可能是特殊设置
        $order['coach_balance'] = $clock_model->getCoachBalance($order, $order['coach_balance']);
        //合伙人
        $order = $coach_model->partnerBalance($coach, $order);
        //渠道商|业务员
        $order = $channel_model->channelBalance($order);
        //分销
        $order = $user_model->getUserPid($order);
        //渠道商分销商佣金是否叠加
        $order = $this->channelFxSuperpose($order);

        return $order;
    }


    /**
     * @功能说明:计算每类佣金的金额
     *
     */
    public function cashData($order, $type)
    {

        $list = $this->commissionData($type);

        $order_data_model = new OrderData();

        $order_data = $order_data_model->dataInfo(['order_id' => $order['id'], 'uniacid' => $order['uniacid']]);

        $order['pay_point'] = $order_data['pay_point'];

        $order['poster_coach_share'] = $order_data['poster_coach_share'];

        $order['poster_admin_share'] = $order_data['poster_admin_share'];

        $order['comm_coach_balance'] = $order_data['comm_coach_balance'];

        $order['skill_balance'] = $order_data['skill_balance'];

        if ($order['material_type'] == 1) {

            $order['true_service_price'] = $order['true_service_price'] + $order['material_price'];
        }
        //广告费
        $order['poster_cash'] = round($order['true_service_price'] * $order_data['poster_point'] / 100, 2);

        foreach ($list as $key => $value) {

            $balance = isset($order[$value['parameter']]) ? $order[$value['parameter']] : 0;

            $order['surplus_cash'] = $key == 0 ? $order['true_service_price'] : $order['surplus_cash'];

            $action_name = $value['action_name'];

            $order = $this->$action_name($balance, $order, $order['surplus_cash']);
        }

        if ($order['material_type'] == 1) {

            $order['true_service_price'] = $order['true_service_price'] - $order['material_price'];
        }

        $order['admin_cash'] = $order['surplus_cash'];
        //如果没有代理商 剩下的就是平台的
        if (empty($order['admin_id'])) {

            $order['company_cash'] = $order['surplus_cash'];
        }
        //计算手续费
        if (!empty($order['pay_point'])) {
            //type 1分销 2加盟商 3技师 4分销商 5上级分销商 6省代分销 7技师拉用户充值余额 8车费 9合伙人 10渠道商 11平台 12业务员 13车费（代理商） 14二级分销
            $arr = [1, 2, 3, 5, 6, 8, 9, 10, 12, 14];

            foreach ($arr as $value) {

                $info = $this->getTypeText($value);
                //说明有这个佣金
                if (isset($order[$info['cash']])) {
                    //手续费
                    $point_cash = round($order[$info['cash']] * $order['pay_point'] / 100, 2);

                    $order[$info['cash']] -= $point_cash;

                    $order[$info['cash']] = round($order[$info['cash']], 2);

                    $order[$info['point']] = $point_cash;
                }
            }
        }

        return $order;

    }


    /**
     * @param $balance
     * @param $order
     * @param $cash 16。28
     * @功能说明:合伙人佣金
     *2.6
     * 市1.4  省1  技师5  合伙人2.6 县5  经济1  渠道商4.4 业务3.4
     */
    public function getPartnerCash($balance, $order, $cash)
    {

        if (!empty($order['partner_id'])) {

            $order['partner_company_cash'] = 0;
            //合伙人提成
            $order['partner_cash'] = round($balance * $order['true_service_price'] / 100, 2);

            $partner_point_cash = $order['partner_cash'] * $order['pay_point'] / 100;

            //   $order['partner_cash'] = $order['partner_cash']-$partner_point_cash;

            $partner_cash = $order['partner_cash'];
            //技师需要分摊的钱
            $coach_cash = round($partner_cash * $order['partner_coach_balance'] / 100, 2);

            $order['partner_cash'] -= $coach_cash;

            $coach_cash = $coach_cash < $order['coach_cash'] ? $coach_cash : $order['coach_cash'];

            $order['partner_cash'] += $coach_cash;

            $order['coach_cash'] -= $coach_cash;
            //技师承担金额
            $order['partner_coach_cash'] = $coach_cash;
            //没有代理商就由平台承担
            if (empty($order['admin_id'])) {
                //平台承担比例
                $order['partner_company_balance'] = 100 > $order['partner_coach_balance'] ? 100 - $order['partner_coach_balance'] : 0;
                //平台需要承担的费用
                $admin_cash = round($partner_cash * $order['partner_company_balance'] / 100, 2);

                $order['partner_cash'] -= $admin_cash;

                $admin_cash = $admin_cash < $order['surplus_cash'] ? $admin_cash : $order['surplus_cash'];

                $order['partner_cash'] += $admin_cash;

                $order['surplus_cash'] -= $admin_cash;
                //平台承担金额
                $order['partner_company_cash'] = $admin_cash;

                $order['partner_admin_balance'] = 0;

            } else {

                $admin_cash = round($partner_cash * $order['partner_admin_balance'] / 100, 2);

                $order['partner_cash'] -= $admin_cash;

                $admin_cash = $admin_cash < $order['surplus_cash'] ? $admin_cash : $order['surplus_cash'];

                $order['partner_cash'] += $admin_cash;

                $order['surplus_cash'] -= $admin_cash;

                $order['partner_admin_cash'] = $admin_cash;

                $compan_balance = $order['partner_company_balance'];

                $order['partner_admin_id'] = $order['admin_id'];

                if ($compan_balance > 0) {

                    $admin_cash = round($partner_cash * $compan_balance / 100, 2);

                    $order['partner_cash'] -= $admin_cash;

                    $admin_cash = $admin_cash < $order['company_cash'] ? $admin_cash : $order['company_cash'];

                    $order['partner_cash'] += $admin_cash;

                    $order['company_cash'] -= $admin_cash;

                    $order['partner_company_cash'] = $admin_cash;
                }
            }

            //  $order['partner_cash'] = $order['partner_cash']+$partner_point_cash;

        }
        return $order;
    }


    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:平台
     *
     */
    public function getCompanyCash($balance, $order, $cash)
    {

        $order['company_cash'] = round($balance * $order['true_service_price'] / 100, 2);

        $order['company_cash'] = $order['company_cash'] > $cash ? $cash : $order['company_cash'];

        $order['surplus_cash'] = $cash - $order['company_cash'];

        return $order;
    }

    /**
     * @param $order
     * @param $cash
     * @功能说明:区县佣金
     */
    public function getCityCash($balance, $order, $cash)
    {
        //上级代理提成
        $order['level_cash'] = round($balance * $order['true_service_price'] / 100, 2);

        $order['level_cash'] = $order['level_cash'] - $cash > 0 ? $cash : $order['level_cash'];

        $order['surplus_cash'] = $cash - $order['level_cash'];
//        //减手续费
//        $order['city_point_cash'] = $order['level_cash']*$order['pay_point']/100;
//
//        $order['level_cash'] = $order['level_cash']-$order['city_point_cash'];

        if (empty($order['admin_id'])) {

            $order['company_cash'] = &$order['surplus_cash'];

            $order['poster_company_share'] = 100 - $order['poster_coach_share'];

        } else {

            $order['poster_company_share'] = 100 - $order['poster_coach_share'] - $order['poster_admin_share'];
//            //减代理商手续费
//            $order['admin_point_cash'] = $order['surplus_cash']*$order['pay_point']/100;
//
//            $order['surplus_cash'] = $order['surplus_cash']-$order['admin_point_cash'];
            //分摊广告费
            $order['share_poster_admin'] = round($order['poster_cash'] * $order['poster_admin_share'] / 100, 2);

            $order['share_poster_admin'] = $order['share_poster_admin'] > $order['surplus_cash'] ? $order['surplus_cash'] : $order['share_poster_admin'];

            $order['surplus_cash'] = $order['surplus_cash'] - $order['share_poster_admin'];

        }
        //手续费 暂时不分摊
        // $order['company_point_cash'] = $order['company_cash']*$order['pay_point']/100;
//        $order['company_point_cash'] = 0;
//
//        $order['company_cash'] = $order['company_cash']-$order['company_point_cash'];

        $order['share_poster_company'] = round($order['poster_cash'] * $order['poster_company_share'] / 100, 2);

        $order['share_poster_company'] = $order['share_poster_company'] > $order['company_cash'] ? $order['company_cash'] : $order['share_poster_company'];

        $order['company_cash'] = $order['company_cash'] - $order['share_poster_company'];

        return $order;

    }

    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:技师佣金
     */

    public function getCoachCash($balance, $order, $cash)
    {
        //技师佣金
        $order['coach_cash'] = round($balance * $order['true_service_price'] / 100, 2);

        $order['coach_cash'] = $order['coach_cash'] > $cash ? $cash : $order['coach_cash'];

        $order['surplus_cash'] = $cash - $order['coach_cash'];

        $coach_cash = $order['coach_cash'];
        //余额支付 技师可能有余额扣款
        if (isset($order['pay_model']) && $order['pay_model'] == 2 && isset($order['comm_coach_balance']) && $order['comm_coach_balance'] > 0) {

            $comm_coach_balance_cash = round($order['coach_cash'] * $order['comm_coach_balance'] / 100, 2);

            $comm_coach_balance_cash = $comm_coach_balance_cash > $order['coach_cash'] ? $order['coach_cash'] : $comm_coach_balance_cash;

            $order['coach_cash'] = $order['coach_cash'] - $comm_coach_balance_cash;

            $order['comm_coach_cash'] = $comm_coach_balance_cash;

        }
        //技术服务费
        if (!empty($order['coach_id']) && isset($order['skill_balance']) && $order['skill_balance'] > 0) {

            $skill_cash = round($coach_cash * $order['skill_balance'] / 100, 2);

            $skill_cash = $skill_cash > $order['coach_cash'] ? $order['coach_cash'] : $skill_cash;

            $order['coach_cash'] = $order['coach_cash'] - $skill_cash;

            $order['skill_cash'] = $skill_cash;

        }
        //第二种物料费规则
        if ($order['material_type'] == 1) {

            $order['coach_cash'] = $order['coach_cash'] - $order['material_price'];

            $order['coach_cash'] = $order['coach_cash'] > 0 ? $order['coach_cash'] : 0;
        }
        //分摊广告费
        $order['share_poster_coach'] = round($order['poster_cash'] * $order['poster_coach_share'] / 100, 2);

        $order['share_poster_coach'] = $order['share_poster_coach'] > $order['coach_cash'] ? $order['coach_cash'] : $order['share_poster_coach'];

        $order['coach_cash'] = $order['coach_cash'] - $order['share_poster_coach'];

        return $order;
    }


    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:省代佣金
     */
    public function getProvinceCash($balance, $order, $cash)
    {
        //上级代理提成
        $order['p_level_cash'] = round($balance * $order['true_service_price'] / 100, 2);

        $order['p_level_cash'] = $order['p_level_cash'] - $cash > 0 ? $cash : $order['p_level_cash'];

        $order['surplus_cash'] = $cash - $order['p_level_cash'];
//        //减手续费
//        $order['province_point_cash'] = $order['p_level_cash']*$order['pay_point']/100;
//
//        $order['p_level_cash'] = $order['p_level_cash']-$order['province_point_cash'];

        return $order;
    }


    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:渠道商佣金
     */
    public function getChannelCash($balance, $order, $cash)
    {

        if (!empty($order['channel_id'])) {

            $order['channel_company_cash'] = 0;
            //上级代理提成
            $order['channel_cash'] = round($balance * $order['true_service_price'] / 100, 2);

            $channel_point_cash = $order['channel_cash'] * $order['pay_point'] / 100;

            // $order['channel_cash'] = $order['channel_cash']-$channel_point_cash;

            $channel_cash = $order['channel_cash'];
            //技师需要分摊的钱
            $coach_cash = round($channel_cash * $order['channel_coach_balance'] / 100, 2);

            $order['channel_cash'] -= $coach_cash;

            $coach_cash = $coach_cash < $order['coach_cash'] ? $coach_cash : $order['coach_cash'];

            $order['channel_cash'] += $coach_cash;

            $order['coach_cash'] -= $coach_cash;
            //技师承担了多少钱
            $order['channel_coach_cash'] = $coach_cash;
            //没有代理商就由平台承担
            if (empty($order['admin_id'])) {
                //平台需要承担的费用
                $admin_cash = round($channel_cash * (100 - $order['channel_coach_balance']) / 100, 2);

                $order['channel_cash'] -= $admin_cash;

                $admin_cash = $admin_cash < $order['surplus_cash'] ? $admin_cash : $order['surplus_cash'];

                $order['channel_cash'] += $admin_cash;

                $order['surplus_cash'] -= $admin_cash;
                //平台承担佣金
                $order['channel_company_cash'] = $admin_cash;

                $order['channel_company_balance'] = 100 - $order['channel_coach_balance'];

                $order['channel_admin_balance'] = 0;

            } else {

                $admin_cash = round($channel_cash * $order['channel_admin_balance'] / 100, 2);

                $order['channel_cash'] -= $admin_cash;
                //上级代理商承担
                if (getConfigSetting($order['uniacid'], 'cash_share_admin') == 1) {

                    $admin_cash = $admin_cash < $order['surplus_cash'] ? $admin_cash : $order['surplus_cash'];

                    $order['channel_cash'] += $admin_cash;

                    $order['surplus_cash'] -= $admin_cash;

                    $order['channel_admin_id'] = $order['admin_id'];

                } else {
                    //由城市代理商承担
                    if (isset($order['city_admin']) && isset($order[$order['city_admin']])) {

                        $admin_cash = $admin_cash < $order[$order['city_admin']] ? $admin_cash : $order[$order['city_admin']];

                        $order['channel_cash'] += $admin_cash;

                        $order[$order['city_admin']] -= $admin_cash;

                        $order['channel_admin_id'] = $order[$order['city_admin_id']];
                    }
                }

                $order['channel_admin_cash'] = $admin_cash;
                //平台承担部分
                $compan_balance = $order['channel_company_balance'];

                if ($compan_balance > 0) {

                    $admin_cash = round($channel_cash * $compan_balance / 100, 2);

                    $order['channel_cash'] -= $admin_cash;

                    $admin_cash = $admin_cash < $order['company_cash'] ? $admin_cash : $order['company_cash'];

                    $order['channel_cash'] += $admin_cash;

                    $order['company_cash'] -= $admin_cash;

                    $order['channel_company_cash'] = $admin_cash;
                }

            }

            //  $order['channel_cash'] = $order['channel_cash']+$channel_point_cash;
        }

        return $order;
    }


    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:业务员佣金
     */
    public function getSalesmanCash($balance, $order, $cash)
    {

        if (!empty($order['salesman_id'])) {
            //上级代理提成
            $order['salesman_cash'] = round($balance * $order['true_service_price'] / 100, 2);

            $salesman_point_cash = $order['salesman_cash'] * $order['pay_point'] / 100;

            // $order['salesman_cash'] = $order['salesman_cash']-$salesman_point_cash;

            $salesman_cash = $order['salesman_cash'];
            //技师需要分摊的钱
            $coach_cash = round($salesman_cash * $order['salesman_coach_balance'] / 100, 2);

            $order['salesman_company_cash'] = 0;

            $order['salesman_cash'] -= $coach_cash;

            $coach_cash = $coach_cash < $order['coach_cash'] ? $coach_cash : $order['coach_cash'];
            //技师承担了多少钱
            $order['salesman_coach_cash'] = $coach_cash;

            $order['salesman_cash'] += $coach_cash;

            $order['coach_cash'] -= $coach_cash;
            //没有代理商就由平台承担
            if (empty($order['admin_id'])) {
                //平台需要承担的费用
                $admin_cash = round($salesman_cash * (100 - $order['salesman_coach_balance']) / 100, 2);
                //平台承担的比例
                $order['salesman_company_balance'] = 100 - $order['salesman_coach_balance'];

                $order['salesman_cash'] -= $admin_cash;

                $admin_cash = $admin_cash < $order['surplus_cash'] ? $admin_cash : $order['surplus_cash'];
                //平台承担的金额
                $order['salesman_company_cash'] = $admin_cash;

                $order['salesman_cash'] += $admin_cash;

                $order['surplus_cash'] -= $admin_cash;

                $order['salesman_admin_balance'] = 0;

            } else {
                $admin_cash = round($salesman_cash * $order['salesman_admin_balance'] / 100, 2);

                $order['salesman_cash'] -= $admin_cash;
                //上级代理商承担
                if (getConfigSetting($order['uniacid'], 'cash_share_admin') == 1) {

                    $admin_cash = $admin_cash < $order['surplus_cash'] ? $admin_cash : $order['surplus_cash'];

                    $order['salesman_cash'] += $admin_cash;

                    $order['surplus_cash'] -= $admin_cash;

                    $order['salesman_admin_id'] = $order['admin_id'];

                } else {
                    //由城市代理商承担
                    if (isset($order['city_admin']) && isset($order[$order['city_admin']])) {

                        $admin_cash = $admin_cash < $order[$order['city_admin']] ? $admin_cash : $order[$order['city_admin']];

                        $order['salesman_cash'] += $admin_cash;

                        $order[$order['city_admin']] -= $admin_cash;

                        $order['salesman_admin_id'] = $order[$order['city_admin_id']];
                    }

                }
                //代理商承担的金额
                $order['salesman_admin_cash'] = $admin_cash;
                //平台承担部分
                $compan_balance = $order['salesman_company_balance'];

                if ($compan_balance > 0) {

                    $admin_cash = round($salesman_cash * $compan_balance / 100, 2);

                    $order['salesman_cash'] -= $admin_cash;

                    $admin_cash = $admin_cash < $order['company_cash'] ? $admin_cash : $order['company_cash'];

                    $order['salesman_cash'] += $admin_cash;

                    $order['company_cash'] -= $admin_cash;
                    //平台
                    $order['salesman_company_cash'] = $admin_cash;
                }

            }

            //  $order['salesman_cash'] = $order['salesman_cash']+$salesman_point_cash;
        }

        return $order;
    }


    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:用户分销
     */
    public function getUserCash($balance, $order, $cash)
    {

        $total_cash = $total_level_cash = 0;

        $reseller_cash_type = getConfigSetting($order['uniacid'], 'reseller_cash_type');

        if (!empty($order['user_top_id'])) {

            $ser_model = new Service();

//            if(!empty($balance)&&$balance>0){
//                //这里是单独客户兼容版本 可以不用管
//                $total_cash = $balance*$order['true_service_price'];
//
//            }else{

            foreach ($order['order_goods'] as &$v) {
                //按服务
                if ($reseller_cash_type == 1) {
                    //查看是否有分销
                    $ser = $ser_model->dataInfo(['id' => $v['goods_id']]);
                    //一级分销
                    $v['user_reseller_balance'] = $ser['com_balance'];
                    //二级分销
                    $v['level_reseller_balance'] = !empty($order['level_top_id']) ? $ser['level_balance'] : 0;
                } else {
                    //一级分销
                    $v['user_reseller_balance'] = $order['user_reseller_balance'];
                    //二级分销
                    $v['level_reseller_balance'] = !empty($order['level_top_id']) ? $order['level_reseller_balance'] : 0;
                }
                //没设置就用全局设置
                if ($v['user_reseller_balance'] < 0) {

                    $v['user_reseller_balance'] = getConfigSetting($order['uniacid'], 'user_agent_balance');

                    $v['user_reseller_balance'] = $v['user_reseller_balance'] > 0 ? $v['user_reseller_balance'] : 0;
                }

                if ($v['level_reseller_balance'] < 0) {

                    $v['level_reseller_balance'] = getConfigSetting($order['uniacid'], 'user_level_balance');

                    $v['level_reseller_balance'] = $v['level_reseller_balance'] > 0 ? $v['level_reseller_balance'] : 0;
                }

                if ($order['material_type'] == 1) {

                    $v['user_reseller_cash'] = ($v['true_price'] + $v['material_price']) * $v['user_reseller_balance'] / 100 * $v['num'];

                    $v['level_reseller_cash'] = ($v['true_price'] + $v['material_price']) * $v['level_reseller_balance'] / 100 * $v['num'];

                } else {

                    $v['user_reseller_cash'] = $v['true_price'] * $v['user_reseller_balance'] / 100 * $v['num'];

                    $v['level_reseller_cash'] = $v['true_price'] * $v['level_reseller_balance'] / 100 * $v['num'];
                }

                $total_cash += $v['user_reseller_cash'];

                $total_level_cash += $v['level_reseller_cash'];
            }
            // }
        }

        $order['user_c_cash'] = $total_cash > $cash ? $cash : $total_cash;

        $order['surplus_cash'] = $cash - $order['user_c_cash'];

        $order['level_reseller_cash'] = $total_level_cash > $order['surplus_cash'] ? $order['surplus_cash'] : $total_level_cash;

        $order['surplus_cash'] = $order['surplus_cash'] - $order['level_reseller_cash'];

        return $order;

    }


    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:升级订单时候的分销
     */
    public function getUserCashUp($balance, $order, $cash)
    {

        $order = $this->getUserCash(0, $order, $cash);

        $this->where(['order_id' => $order['id']])->where('type', 'in', [1, 14])->delete();


        return $order;
    }


    /**
     * @功能说明:退款的时候要减去分销
     */
    public function getUserCashRefund($balance, $order, $cash)
    {
        //一级分销
        $order = $this->getUserCashRefundData($order, $cash, 1);
        //二级分销
        $order = $this->getUserCashRefundData($order, $order['surplus_cash'], 14);

        return $order;
    }


    /**
     * @功能说明:退款的时候要减去分销
     */
    public function getUserCashRefundData($order, $cash, $type = 1)
    {

        $refund_model = new RefundOrder();

        $com_goods_mdoel = new CommissionGoods();

        $refund_order = $refund_model->dataInfo(['id' => $order['refund_id']]);

        $total_cash = 0;

        if (!empty($refund_order)) {
            //查询这笔等待有无佣金
            $comm = $this->dataInfo(['order_id' => $refund_order['order_id'], 'status' => 1, 'type' => $type]);

            if (!empty($comm)) {

                foreach ($refund_order['order_goods'] as $v) {

                    $comm_goods = $com_goods_mdoel->dataInfo(['commission_id' => $comm['id'], 'order_goods_id' => $v['order_goods_id']]);

                    if (!empty($comm_goods) && $comm_goods['num'] > 0) {

                        $comm_goods_cash = $comm_goods['cash'] / $comm_goods['num'];

                        $true_num = $comm_goods['num'] - $v['num'];

                        $true_num = $true_num > 0 ? $true_num : 0;

                        $update = [

                            'num' => $true_num,

                            'cash' => $comm_goods_cash * $true_num
                        ];

                        $com_goods_mdoel->dataUpdate(['id' => $comm_goods['id']], $update);
                    }
                }

                $total_cash = $com_goods_mdoel->where(['commission_id' => $comm['id']])->sum('cash');

                $total_cash = $total_cash > 0 ? $total_cash : 0;
            }
        }

        if ($type == 1) {

            $order['user_c_cash'] = $total_cash > $cash ? $cash : $total_cash;

            $order['surplus_cash'] = $cash - $order['user_c_cash'];
        } else {

            $order['level_reseller_cash'] = $total_cash > $cash ? $cash : $total_cash;

            $order['surplus_cash'] = $cash - $order['level_reseller_cash'];
        }

        return $order;

    }


    /**
     * @功能说明:添加佣金
     */
    public function commissionAddDataV2($order)
    {

        if (!empty($order['user_top_id'])) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [1])->find();

            if (!empty($find)) {

                return false;
            }

            $com_mdoel = new Commission();

            $com_goods_mdoel = new CommissionGoods();

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id' => $order['user_top_id'],

                'reseller_id' => $order['user_reseller_id'],

                'order_id' => $order['id'],

                'order_code' => $order['order_code'],

                'balance' => $order['user_reseller_balance'],

                'cash' => $order['user_c_cash'],

                'status' => -1,

                'admin_id' => !empty($order['admin_id']) ? $order['admin_id'] : 0
            ];

            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $com_mdoel->dataAdd($insert);

            $id = $com_mdoel->getLastInsID();

            foreach ($order['order_goods'] as $v) {

                $insert = [

                    'uniacid' => $order['uniacid'],

                    'order_goods_id' => $v['id'],

                    'commission_id' => $id,

                    'cash' => $v['user_reseller_cash'],

                    'num' => $v['num'],

                    'balance' => $v['user_reseller_balance']
                ];
                //添加到自订单记录表
                $res = $com_goods_mdoel->dataAdd($insert);
            }

            $share_model = new CommShare();
            //添加手续费
            $share_model->addPointData($id, $order);
        }
        return true;
    }


    /**
     * @功能说明:二级分销添加佣金
     */
    public function commissionLevelAddData($order)
    {

        if (!empty($order['level_top_id'])) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [14])->find();

            if (!empty($find)) {

                return false;
            }

            $com_mdoel = new Commission();

            $com_goods_mdoel = new CommissionGoods();

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_top_id'],

                'top_id' => $order['level_top_id'],

                'reseller_id' => $order['level_reseller_id'],

                'sub_reseller_id' => $order['user_reseller_id'],

                'order_id' => $order['id'],

                'order_code' => $order['order_code'],

                'balance' => $order['level_reseller_balance'],

                'cash' => $order['level_reseller_cash'],

                'status' => -1,

                'admin_id' => !empty($order['admin_id']) ? $order['admin_id'] : 0,

                'type' => 14
            ];

            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $com_mdoel->dataAdd($insert);

            $id = $com_mdoel->getLastInsID();

            foreach ($order['order_goods'] as $v) {

                $insert = [

                    'uniacid' => $order['uniacid'],

                    'order_goods_id' => $v['id'],

                    'commission_id' => $id,

                    'cash' => $v['level_reseller_cash'],

                    'num' => $v['num'],

                    'balance' => $v['level_reseller_balance']
                ];
                //添加到自订单记录表
                $res = $com_goods_mdoel->dataAdd($insert);
            }

            $share_model = new CommShare();
            //添加手续费
            $share_model->addPointData($id, $order);
        }

        return true;

    }


    /**
     * @param $order
     * @功能说明:增加合伙人佣金
     */
    public function partnerCommission($order)
    {

        if (!empty($order['partner_id']) && isset($order['coach_agent_balance']) && !empty($order['broker_id'])) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [9])->find();

            if (!empty($find)) {

                return false;
            }

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id' => $order['partner_id'],

                'broker_id' => $order['broker_id'],

                'order_id' => $order['id'],

                'order_code' => $order['order_code'],

                'type' => 9,

                'cash' => $order['partner_cash'],

                'admin_id' => !empty($order['admin_id']) ? $order['admin_id'] : 0,

                'balance' => $order['coach_agent_balance'],

                'status' => -1,
            ];
            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $res = $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $share_model = new CommShare();
            //添加技师分摊比例
            if (!empty($order['partner_coach_balance'])) {

                $share_model->addData($order['uniacid'], $order['partner_coach_balance'], $order['partner_coach_cash'], $id, 1, $order['id'], $order['coach_id']);

            }
            //添加代理商分摊比例
            if (!empty($order['partner_admin_balance'])) {

                $share_model->addData($order['uniacid'], $order['partner_admin_balance'], $order['partner_admin_cash'], $id, 2, $order['id'], $order['partner_admin_id']);

            }
            //添加平台分摊比例
            if (!empty($order['partner_company_balance'])) {

                $share_model->addData($order['uniacid'], $order['partner_company_balance'], $order['partner_company_cash'], $id, 3, $order['id']);

            }

            $share_model = new CommShare();
            //添加手续费
            $share_model->addPointData($id, $order);
        }

        return true;

    }


    /**
     * @param $order
     * @功能说明:增加渠道商佣金
     */
    public function channelCommission($order)
    {

        if (!empty($order['channel_id']) && isset($order['channel_balance'])) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [10])->find();

            if (!empty($find)) {

                return false;
            }
            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id' => $order['channel_id'],

                'order_id' => $order['id'],

                'order_code' => $order['order_code'],

                'type' => 10,

                'cash' => $order['channel_cash'],

                'balance' => $order['channel_balance'],

                'admin_id' => !empty($order['admin_id']) ? $order['admin_id'] : 0,

                'channel_qr_id' => !empty($order['channel_qr_id']) ? $order['channel_qr_id'] : 0,

                'status' => -1,

            ];
            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $res = $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $share_model = new CommShare();
            //添加技师分摊比例
            if (!empty($order['channel_coach_balance'])) {

                $share_model->addData($order['uniacid'], $order['channel_coach_balance'], $order['channel_coach_cash'], $id, 1, $order['id'], $order['coach_id']);

            }
            //添加代理商分摊比例
            if (!empty($order['channel_admin_balance'])) {

                $share_model->addData($order['uniacid'], $order['channel_admin_balance'], $order['channel_admin_cash'], $id, 2, $order['id'], $order['channel_admin_id']);

            }
            //添加平台分摊比例
            if (!empty($order['channel_company_balance'])) {

                $share_model->addData($order['uniacid'], $order['channel_company_balance'], $order['channel_company_cash'], $id, 3, $order['id']);

            }

            $share_model = new CommShare();
            //添加手续费
            $share_model->addPointData($id, $order);

        }

        return true;

    }


    /**
     * @param $order
     * @功能说明:增加渠道商佣金
     */
    public function salesmanCommission($order)
    {

        if (!empty($order['salesman_id']) && isset($order['salesman_balance'])) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [12])->find();

            if (!empty($find)) {

                return false;
            }

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id' => $order['salesman_id'],

                'order_id' => $order['id'],

                'order_code' => $order['order_code'],

                'type' => 12,

                'cash' => $order['salesman_cash'],

                'balance' => $order['salesman_balance'],

                'admin_id' => !empty($order['admin_id']) ? $order['admin_id'] : 0,

                'status' => -1,

            ];
            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $res = $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $share_model = new CommShare();
            //添加技师分摊比例
            if (!empty($order['salesman_coach_balance'])) {

                $share_model->addData($order['uniacid'], $order['salesman_coach_balance'], $order['salesman_coach_cash'], $id, 1, $order['id'], $order['coach_id']);

            }
            //添加代理商分摊比例
            if (!empty($order['salesman_admin_balance'])) {

                $share_model->addData($order['uniacid'], $order['salesman_admin_balance'], $order['salesman_admin_cash'], $id, 2, $order['id'], $order['salesman_admin_id']);

            }
            //添加平台分摊比例
            if (!empty($order['salesman_company_balance'])) {

                $share_model->addData($order['uniacid'], $order['salesman_company_balance'], $order['salesman_company_cash'], $id, 3, $order['id']);

            }
            $share_model = new CommShare();
            //添加手续费
            $share_model->addPointData($id, $order);

        }

        return true;

    }


    /**
     * @param $order
     * @功能说明:增加平台佣金
     */
    public function companyCommission($order)
    {

        if (isset($order['company_cash'])) {

            $find = $this->where(['order_id' => $order['id']])->where('type', 'in', [11])->find();

            if (!empty($find)) {

                return false;
            }

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id' => 0,

                'order_id' => $order['id'],

                'order_code' => $order['order_code'],

                'type' => 11,

                'cash' => $order['company_cash'],

                'balance' => 0,

                'status' => -1,
            ];
            $insert['create_time'] = !empty($order['create_time']) ? $order['create_time'] : time();

            $res = $this->dataAdd($insert);

            $id = $this->getLastInsID();

            $share_model = new CommShare();
            //添加手续费
            //  $share_model->addPointData($id,$order);

            if (isset($order['share_poster_company'])) {
                //添加广告费
                $share_model->addPosterData($id, $order, 3);
            }
        }
        return true;
    }


    /**
     * @param $order_id
     * @param $order
     */
    public function getFinanceText($order)
    {

        $order_id = array_column($order['data'], 'order_id');

        $dis[] = ['order_id', 'in', $order_id];

        $dis[] = ['status', '>', -1];

        $list = $this->where($dis)->select()->toArray();

        $dataPath = APP_PATH . 'massage/info/FinanceText.php';

        $text = include $dataPath;

        $change_model = new CoachChangeLog();

        $share_model = new CommShare();

        if (!empty($list)) {

            foreach ($order['data'] as &$values) {
                //总的真实的支付金额包含车费
                $values['remain_cash'] = $values['true_service_price'];

                foreach ($text as $vv) {

                    $values[$vv['cash']] = 0;

                    $values[$vv['name']] = '';

                }
                foreach ($list as $value) {

                    if ($values['order_id'] == $value['order_id']) {

                        foreach ($text as $vs) {

                            if (in_array($value['type'], $vs['type'])) {

                                if (empty($vs['city_type']) || (!empty($vs['city_type']) && $value['city_type'] == $vs['city_type'])) {
                                    //代理商的佣金可能会包含 线下技师的佣金和车费
                                    if ($value['type'] == 2) {

                                        $value['cash'] = $value['cash'] - $value['coach_cash'] - $value['car_cash'];
                                    }

                                    $values[$vs['cash']] = !empty($value) ? $value['cash'] : 0;

                                    $obj_top_id = isset($vs['top_id']) ? $vs['top_id'] : 'top_id';

                                    $values[$vs['name']] = Db::name($vs['table'])->where(['id' => $value[$obj_top_id]])->value($vs['title']);
                                    //佣金对应者名称
                                    $values[$vs['name']] = !empty($values[$vs['name']]) ? $values[$vs['name']] : '';

                                    $values['remain_cash'] -= $values[$vs['cash']];
                                }
                            }
                        }
                    }
                }
                //这里需要兼容一下老版本,以前的车费没有佣金记录
                $values['car_cash'] = $values['car_price'];

                $car_share = $share_model->where(['order_id' => $values['order_id']])->where('comm_type', 'in', [8, 13])->sum('share_cash');

                if (!empty($car_share)) {

                    $values['car_cash'] .= '(含手续费' . $car_share . ')';
                }
                $values['remain_cash'] = round($values['remain_cash'], 2);
                //线下技师
                if (empty($values['coach_id'])) {

                    $values['coach_name'] = $change_model->getCoachByOrder($values['order_id']);
                }
            }
        }

        return $order;
    }


    /**
     * @param $order
     * @功能说明:渠道商佣金分销商佣金是否叠加
     * @author chenniang
     * @DataTime: 2023-11-14 15:54
     */
    public function channelFxSuperpose($order)
    {

        $channel_fx_superpose = getConfigSetting($order['uniacid'], 'channel_fx_superpose');

        if ($channel_fx_superpose == 1 || empty($order['user_top_id']) || empty($order['channel_id'])) {

            return $order;
        }

        $fx_bind_model = new QrBind();

        $channel_bind_model = new UserChannel();

        //  $user_mdoel = new User();

        //  $user_info  = $user_mdoel->dataInfo(['id'=>$order['user_id']]);

//        if(empty($user_info)){
//
//            return $order;
//        }
        //分销绑定时间
        // $fx_bind_time = $fx_bind_model->where(['uniacid'=>$order['uniacid'],'openid'=>$user_info['web_openid']])->max('create_time');

//        $fx_bind_time = !empty($fx_bind_time)?$fx_bind_time:$user_info['create_time'];
//
//        $where = [
//
//            'user_id' => $order['user_id'],
//
//            'open_id' => $user_info['web_openid']
//        ];
        //渠道商绑定时间
//        $channel_bind_time = $channel_bind_model->where(['uniacid'=>$order['uniacid']])->where(function ($query) use ($where){
//            $query->whereOr($where);
//        })->max('create_time');
        //1 渠道商有佣金 2 分销商有佣金
//        if($fx_bind_time<$user_info['create_time']&&$channel_bind_time<$user_info['create_time']){
//
//            if($fx_bind_time<$channel_bind_time){
//
//                $first = 1;
//
//            }else{
//
//                $first = 2;
//            }
//        }else{
//
//            if($fx_bind_time>$channel_bind_time){
//
//                $first = 1;
//
//            }else{
//
//                $first = 2;
//            }
//        }

        $first = 1;

        if (!empty($order['user_top_id'])) {

            $first = 2;
        }

        if ($first == 1) {

            $order['user_top_id'] = 0;

            $order['level_top_id'] = 0;

        } else {

            $order['channel_id'] = 0;

            $order['salesman_id'] = 0;

            $order_model = new Order();

            $order_model->dataUpdate(['id' => $order['id']], ['channel_id' => 0, 'salesman_id' => 0]);
        }

        return $order;
    }


}